package com.controller.manager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.po.AdminBeanW;
import com.service.manager.AdminServiceW;

@Controller
@RequestMapping("/jsp")
public class AdminController {

	@Resource(name="adminServiceImpW")
	private AdminServiceW adminService;
	
	@RequestMapping(value="/login")
	public String checkLogin(AdminBeanW admin,Model model,HttpServletRequest request){
		HttpSession session = request.getSession();
		String checkcode = session.getAttribute("rand").toString();
		int flag = adminService.findAdmin(admin, checkcode);
		String result = "";
		switch(flag){
		case 0: 
			model.addAttribute("error", "验证码错误");
			model.addAttribute("admin", admin);
			result = "login";
			break;
		case 1:
			model.addAttribute("error", "用户名或者密码错误");
			model.addAttribute("admin", admin);
			result = "login";
			break;
		case 2:
			session.setAttribute("admin", admin);
			model.addAttribute("admin", admin);
			result = "main";
			break;
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value="/getAdminList")
	public List<AdminBeanW> getAdminList(AdminBeanW admin,Integer page,Integer rows){
		
		admin.setPage((page-1)*rows);
		admin.setRows(rows);
		List<AdminBeanW> list = adminService.getAdminList(admin);
		int count = adminService.getAdminCount(admin);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("total", count);
		map.put("rows", list);
		return list;
	}
	
	@ResponseBody
	@RequestMapping(value="/addAdmin")
	public int addAdminBean(AdminBeanW admin){
		
		int result = adminService.addAdmin(admin);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value="/updateAdmin")
	public int updateAdminBean(AdminBeanW admin){
		
		int result = adminService.updateAdmin(admin);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value="/delAdmin")
	public int delAdminList(String adminids){
		
		String[] array = adminids.split(",");
		int result = adminService.delAdminList(array);
		return result;
	}
	@ResponseBody
	@RequestMapping(value="/updateAdpwd")
	public int updateAdmin(AdminBeanW admin){
		
		int result = adminService.updateAdmin(admin);
		return result;
	}
	
	@RequestMapping(value="/logout")
	public String loginOut(HttpServletRequest request){
		HttpSession session = request.getSession();
		session.removeAttribute("admin");
		return "login";
		
	}
}
